﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using msBsc.SvRef;
using System.Data;

namespace msBsc.WebUi.wMall.Public
{
    public partial class wfIndex : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }



        //登录
        private void Login()
        {
            string account = this.GetParamHtValue("account");
            string password = this.GetParamHtValue("password");
            string code = this.GetParamHtValue("code");
            if (string.IsNullOrEmpty(account))
            {
                this.ExecJScript("ShowErrMsg('用户名不能为空')");
                return;
            }

            if (string.IsNullOrEmpty(password))
            {
                this.ExecJScript("ShowErrMsg('密码不能为空')");
                return;
            }

            //if (!CString.IsEmail(account.Trim()))
            //{
            //    this.ExecJScript("ShowErrMsg('账户只能是邮箱')");
            //    return;
            //}

            if (!CString.IsPassword(password.Trim()))
            {
                this.ExecJScript("ShowErrMsg('密码只能是6~16位')");
                return;
            }

            //if (code != (string)GetSession(this.sCurrPageRadom + msBsc.Cls.ValidateCode.VALIDATECODEKEY, "2"))
            //{
            //    this.ExecJScript("jQuery.getValidateImg('imgValidata');");
            //    this.SetContrlValue("login_notice", "验证码错误或已过期，请重新录入！");
            //    return;
            //}


            string ret = UserInfo.LoginCustomer(account, password);
            password = msBsc.AppLogic.Cfg.DesCry.encMe(password, "zbhpass");
            if (ret != "OK")
            {
                if (ret == "OK1")
                {
                    this.ExecJScript("setCookie('" + account + "','" + password + "');window.location.href='/wp_ModifyAgentDjackPwd.htm';");
                    return;
                }

                this.ExecJScript("ShowErrMsg('" + ret + "')");
                return;
            }
            else
            {
                if (UserInfo.OperType=="3")
                {
                    this.ExecJScript("setCookie('" + account + "','" + password + "');window.location.href='/ws_AgentDjackInfo.htm';");
                    return;
                }
                this.ExecJScript(@"setCookie('" + account + "','" + password + "');susccLogin()");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                //string password = this.GetParamHtValue("password");

                //if (!msBsc.SvRef.UserInfo.Online)
                //{
                //    this.ExecJScript(@"$("".leader-login-wrap"").show();");
                //}
                //if (!string.IsNullOrEmpty(password))
                //{
                //    password = msBsc.AppLogic.Cfg.DesCry.uncMe(password, "zbhpass");
                //    this.ExecJScript(@"$('#pwdPassword').val('" + password + "')");
                //}
                string parentsId = this.GetParamHtValue("parentsId");
                getArea2(parentsId);
                ShowButiqueInfo();
                //ShowButiqueRecomm();
                GetClassicFood();
                GetClassicSpecialty();
                GetPagedDjackInfo();
                getPopularCity();

            }
            else if (this.strAction == "login")
            {
                Login();
            }
            else if (this.strAction=="upcity")
            {
                string parentsId = this.GetParamHtValue("parentsId");
                getArea3(parentsId);
            }

        }

        string countryId = "";
        string cityId = "";
        string destionId = "";
        private void getArea2(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                cityId = this.GetParamHtValue("cityId");
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                if (string.IsNullOrEmpty(cityId))
                {
                    cityId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                this.ExecJScript(@"updateSelet(""cityId_select""," + ret + @",""" + cityId + @""")");
                getArea3(cityId);
            }
        }
        private void getArea3(string parentsId)
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.SelectCountryByParentId(parentsId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                destionId = this.GetParamHtValue("destionId");
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                this.ExecJScript(@"updateSelet(""destionId_select""," + ret + @",""" + destionId + @""")");

            }
        }

        private void ShowButiqueInfo()
        {
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo objCBizVehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                System.Data.DataSet ds = new System.Data.DataSet();

                msBsc.AppLogic.DataAlc.CBizCountryCity araeService = new AppLogic.DataAlc.CBizCountryCity();

                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("countryid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueCountry")));
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("cityid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueCity")));
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("desid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueDestin")));


                ds = objCBizVehicleInfo.DataObj.QueryBySQL(@"SELECT TOP 4 
                                                            A.VEHICLE_PRICE_DAY
                                                            ,A.VEHICLE_DEFAULT_PIC
                                                            ,A.VEHICLE_INFO_ID
                                                            ,A.VEHICLE_NAME
                                                            ,A.VEHICLE_BRAND
                                                            ,A.VEHICLE_SERVICE_TYPE
                                                            ,A.VEHICLE_SEATS
                                                            FROM DJK_VEHICLE_INFO A
                                                            WHERE (A.VEHICLE_RMK <> 'T' OR A.VEHICLE_RMK IS NULL)
																AND (@countryid='' OR A.VEHICLE_COUNTRY=@countryid)
																AND (@cityid='' OR A.VEHICLE_CITY=@cityid)
																AND (@desid='' OR A.VEHICLE_DESTINATION=@desid)
                                                            ORDER BY A.VEHICLE_PUBLISH_TIME DESC", parmCollection);

                ds.Tables[0].Columns.Add(new System.Data.DataColumn("VEHICLE_BRAND_NAME", typeof(string)));
                ds.Tables[0].Columns.Add(new System.Data.DataColumn("VEHICLE_SERVICETYPE_NAME", typeof(string)));
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), ds.Tables[0].Rows[i]["VEHICLE_BRAND"]);
                    ds.Tables[0].Rows[i]["VEHICLE_SERVICETYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), ds.Tables[0].Rows[i]["VEHICLE_SERVICE_TYPE"]);
                }

                string execJsJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 200, "");

                this.ExecJScript(@" var _html = $('#vehicle_list_temp').html();
                            laytpl(_html).render(" + execJsJson + @", function (render) {
                                                $('#vehicleList').html(render);
                            });");
            }
        }

        private void ShowButiqueRecomm()
        {
            using (msBsc.AppLogic.DataAlc.CBizPushScenic objCBizPushScenic = new AppLogic.DataAlc.CBizPushScenic())
            {
                System.Data.DataSet ds = new System.Data.DataSet();
                msBsc.AppLogic.DataAlc.CBizCountryCity araeService = new AppLogic.DataAlc.CBizCountryCity();

                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("countryid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueCountry")));
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("cityid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueCity")));
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("desid", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, araeService.GetDefaultAreaInfo("ButiqueDestin")));

                ds = objCBizPushScenic.DataObj.QueryBySQL(@"SELECT TOP 7 
                                                    A.SCENIC_ID
                                                    ,A.SCENIC_NAME
                                                    ,A.SCENIC_DEFAULT_PIC 
                                                    FROM DJK_PUSH_SCENIC A
                                                    WHERE A.SCENIC_IS_SPECIE=1
	                                                AND A.SCENIC_IS_DELETED=0 
	                                                AND A.SCENIC_IS_DISPLAY=1
													AND (@countryid='' OR A.SCENIC_COUNTRY=@countryid)
													AND (@cityid='' OR A.SCENIC_CITY=@cityid)
													AND (@desid='' OR A.SCENIC_DISTINATION=@desid)
                                                    ORDER BY A.SCENIC_UPLOAD_TIME DESC", parmCollection);
                string execJsJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 200, "");

                this.ExecJScript("ShowRecomm(" + execJsJson + ")");
            }
        }

        private void GetClassicFood()
        {
            using (msBsc.AppLogic.DataAlc.CBizPushNiceFood food = new AppLogic.DataAlc.CBizPushNiceFood())
            {
                DataSet ds = food.GetNiceFoodForPageview(4, 1, "", "", "");
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 20, "");
                this.ExecJScript(@"
                                            var _html = $('#svc_list_tem_food').html();
                            laytpl(_html).render(" + strJson + @", function (render) {
                                                $('#foodList').html(render);
                            });");
            }
        }

        private void GetClassicSpecialty()
        {
            using (msBsc.AppLogic.DataAlc.CBizPushSpecialty specialty = new AppLogic.DataAlc.CBizPushSpecialty())
            {
                DataSet ds = specialty.GetSpecialForPageview(4, 1, "", "", "");
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 20, "");
                this.ExecJScript(@"
                                            var _html = $('#svc_list_tem_specialty').html();
                                            laytpl(_html).render(" + strJson + @", function (render) {
                                                $('#specialtyList').html(render);
                                            });");
            }
        }

        private void GetPagedDjackInfo()
        {
            string sCondition = "";
            int pagesize = 4;

            int ipageIndex = 1;

            sCondition = string.IsNullOrEmpty(sCondition) ? string.Empty : "%" + sCondition + "%";

            using (msBsc.AppLogic.DataAlc.CBizCustomerRegister objCBizCustomerRegister = new AppLogic.DataAlc.CBizCustomerRegister())
            {
                Epm.AppLogic.Framework.CDbParmCollection parm = new Epm.AppLogic.Framework.CDbParmCollection();

                parm.Add(new Epm.AppLogic.Framework.CDbParm("ipageindex", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.Integer, 8, ipageIndex));
                parm.Add(new Epm.AppLogic.Framework.CDbParm("ipagesize", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.Integer, 8, pagesize));
                parm.Add(new Epm.AppLogic.Framework.CDbParm("keywords", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 1000, sCondition.Replace(",", "','").Replace("'", "")));

                System.Data.DataSet ds = objCBizCustomerRegister.DataObj.QueryBySP("DJK_SELECT_DJACK_LISTINFO_FOR_VIEW", parm);
               

                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 20, "");
                //this.ExecJScript(@"SetDjackList(" + strJson + "," + pageCount + "," + datarowCount + ");");
                this.ExecJScript(@"
                                            var _html = $('#user_list_temp').html();
                            laytpl(_html).render(" + strJson + @", function (render) {
                                                $('#userList').html(render);
                            });");

            }
        }

        private void getPopularCity()
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                DataSet ds = cbcc.DataObj.QueryBySQL("SELECT TOP 4 COUNTRY_CITY_ID,COUNTRY_CITY_NME,CITY_CARD FROM DJK_COUNTRY_CITY WHERE CITY_IS_DELETED=0 AND CITY_IS_DISPLAY=1 AND CITY_IS_TOP=1 order by COUNTRY_CITY_SRT asc");
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"$('#travel_list').html('');");
                    return;
                }
                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                this.ExecJScript(@" var _html = $('#city_list_temp').html();
                            laytpl(_html).render(" + strJson + @", function (render) {
                                                $('#popCity_list').html(render);
                            });");
            }
        }

    }
}